#include <stdio.h>
#include <stdlib.h>

#include "Common.h"

typedef char ElemType;
typedef struct BiTNode {
    ElemType data;
    struct BiTNode *lChild;
    struct BiTNode *rChild;
} BiTNode;

typedef BiTNode *BiTree;

int count = 0;

void initTree(ElemType data, BiTree tree, BiTree lChild, BiTree rChild);

void preOrderTraverse(BiTree tree);

void inOrderTraverse(BiTree tree);

void postOrderTraverse(BiTree tree);

void createTree(BiTree *tree);

int main() {
    // BiTNode nodeK = {'K', NULL, NULL};
    // BiTNode nodeH = {'H', NULL, &nodeK};
    // BiTNode nodeD = {'D', &nodeH, NULL};
    // BiTNode nodeE = {'E', NULL, NULL};
    // BiTNode nodeB = {'B', &nodeD, &nodeE};
    // BiTNode nodeI = {'I', NULL, NULL};
    // BiTNode nodeF = {'F', &nodeI, NULL};
    // BiTNode nodeJ = {'J', NULL, NULL};
    // BiTNode nodeG = {'G', NULL, &nodeJ};
    // BiTNode nodeC = {'C', &nodeF, &nodeG};
    // BiTNode nodeA = {'A', &nodeB, &nodeC};

    // BiTree tree = &nodeA;
    BiTree tree;
    createTree(&tree);

    // preOrderTraverse(tree);
    // printf("\n");

    // inOrderTraverse(tree);
    // printf("\n");

    // postOrderTraverse(tree);
    return 0;
}

void createTree(BiTree *tree) {
    ElemType data = '#';
    scanf("%c", &data);
    printf("%d\n", ++count);
    if (data == '#') {
        printf("NULL TREE\n");
        *tree = NULL;
    } else {
        *tree = (BiTree)malloc(sizeof(BiTNode));
        if (!*tree) {
            exit(EXIT_SUCCESS);
        }
        (*tree)->data = data;
        printf("LOOP TREE\n");
        createTree(&(*tree)->lChild);
        createTree(&(*tree)->rChild);
        printf("ended\n");
    }
}

void postOrderTraverse(BiTree tree) {
    if (tree == NULL) {
        return;
    }
    postOrderTraverse(tree->lChild);
    postOrderTraverse(tree->rChild);
    printf("%c ", tree->data);
}

void inOrderTraverse(BiTree tree) {
    if (tree == NULL) {
        return;
    }
    inOrderTraverse(tree->lChild);
    printf("%c ", tree->data);
    inOrderTraverse(tree->rChild);
}

void preOrderTraverse(BiTree tree) {
    if (tree == NULL) {
        return;
    }
    printf("%c ", tree->data);
    preOrderTraverse(tree->lChild);
    preOrderTraverse(tree->rChild);
}

void initTree(ElemType data, BiTree tree, BiTree lChild, BiTree rChild) {
    tree->data = data;
    tree->lChild = lChild;
    tree->rChild = rChild;
}
